﻿using System;
using Accounting.Library;
using Accounting.Library.Utility;
using Csla.Xaml;
using Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu
{
    public class BSP_BanDichVu_ViewModel : ViewModel<Accounting.Library.BSP_BanDichVu>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);
        public delegate void DieuKienNghiepVuChanged_EventHandler(object sender, DieuKienNghiepVuEventArgs e);
        public delegate void LoadCompleted_EventHandler(object sender, LoadedEventArgs e);
        
        public event EventHandler Saved;
        public event EventHandler Canceled;

        public event LoadCompleted_EventHandler LoadCompleted;
        public event EventHandler LoaiTienTeChanged;
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;
        public event DieuKienNghiepVuChanged_EventHandler DieuKienNghiepVuChanged;

        private bool isOnSaveAndNewFlag = false;
        private bool isOnBoghiFlag = false;
        private bool isOnSaveFlag = false;
        #endregion

        #region Refresh Model
        public BSP_BanDichVu_ViewModel()
        {
            BeginRefresh("NewBSP_BanDichVu");            
        }

        public BSP_BanDichVu_ViewModel(int Khoa)
        {
            BeginRefresh("GetBSP_BanDichVu", Khoa);           
        }

        public void NewBSP_BanDichVuViewModel()
        {
            BeginRefresh("NewBSP_BanDichVu");            
        }

        public void GetBSP_BanDichVuViewModel(int Khoa)
        {
            BeginRefresh("GetBSP_BanDichVu", Khoa);
        }

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }

            if (Model != null)
            {
                BeginRefresh("GetBSP_BanDichVu", Model.Khoa);
                isOnSaveFlag = true;
            }

            if (isOnSaveAndNewFlag)
            {
                NewBSP_BanDichVuViewModel();
                isOnSaveAndNewFlag = false;
            }
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
        }

        public void SaveAndNew()
        {
            BeginSave();
            isOnSaveAndNewFlag = true;
        }

        public void SaveBanDichVu()
        {
            BeginSave();
            isOnSaveAndNewFlag = false;
        }

        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                InitinalizeEventHandler();

                if (LoadCompleted != null)
                {
                    if (isOnBoghiFlag) 
                    {
                        LoadCompleted(this, new LoadedEventArgs(LoadModeEnum.ForBoGhi));
                        isOnBoghiFlag = false;
                    }
                    else if (isOnSaveFlag)
                    {
                        LoadCompleted(this, new LoadedEventArgs(LoadModeEnum.ForSave));
                        isOnSaveFlag = false;
                    }
                    else 
                    {
                        LoadCompleted(this, new LoadedEventArgs(LoadModeEnum.ForNew));
                    }
                }
            }
        }

        private void InitinalizeEventHandler() 
        {
            Model.BSP_BanDichVu_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanDichVu_ChiTietChild>>(BSP_BanDichVu_ChiTiets_AddedNew);
            Model.BSP_BanDichVu_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<BSP_BanDichVu_ThueChild>>(BSP_BanDichVu_Thues_AddedNew);
            Model.DieuKienNghiepVuChanged += new EventHandler<DieuKienNghiepVuEventArgs>(OnDieuKienNghiepVuChanged);
            Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
            Model.LoaiTienTeChanged += new EventHandler(OnLoaiTienTeChanged);
        }
        #endregion
        
        #region Add BSP_BanDichVu Details Item
        public void AddBSP_BanDichVu_ChiTietItem()
        {
            Model.BSP_BanDichVu_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddBSP_BanDichVu_ThueItem()
        {
            Model.BSP_BanDichVu_Thues.AddNewItem(Model.Khoa);
        }   

        void BSP_BanDichVu_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanDichVu_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanDichVu_ChiTiets"));
        }      

        void BSP_BanDichVu_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<BSP_BanDichVu_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("BSP_BanDichVu_Thues"));
        }

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }
        }

        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

        public void OnDieuKienNghiepVuChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
            if (DieuKienNghiepVuChanged != null)
            {
                DieuKienNghiepVuChanged(sender, e);
            }
        }

        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }
        #endregion

        #region Delete BSP_BanDichVu Details Item
        public void DeleteBSP_BanDichVu_ChiTietItem(int Index)
        {
            Model.BSP_BanDichVu_ChiTiets.DeleteItem(Index);
        }

        public void DeleteBSP_BanDichVu_ThueItem(int Index)
        {
            Model.BSP_BanDichVu_Thues.DeleteItem(Index);
        }
        #endregion

        #region Nghiep Vu So
        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu;
            int chungTuGocId = Model.Khoa;
            int nhaCungCapId = (int)Model.NhaCungCapId;
            int phuongThucThanhToanId = (int)Model.PhuongThucThanhToanId;
            double soTien = (double)Model.TongTienThanhToanQĐ;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, nhaCungCapId, phuongThucThanhToanId, soTien, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;

                BeginRefresh("GetBSP_BanDichVu", Model.Khoa);
                isOnBoghiFlag = true;

                this.IsBusy = false;
            });            
        }
#endregion
    }
}
